// pages/lost/lost.js


import API from "../../data/api";
import Util from "../../utils/util";

const App = getApp();
const Config = App.Config;

Page({
  data: {
    // 全部
    allPage: 0,
    allListData: [],

    // 寻物
    lostPage: 0,
    lostListData: [],

    //二手
    buyPage: 0,
    buyListData: [],

    //吐槽
    chatPage: 0,
    chatListPage: [],

    //搜索输入框是否展开
    searchInput: false,
    searchInputWorld: '',

    postAnimation: '', // 添加动画效果

    HOSTIMG: Config.HOSTIMG, // 图片链接
    currentNav: 0,  // tab切换  
  },

  onReady: function () {
    var that = this;
    // 初始化数据
    that.setData({
      allPage: 0,
      allListData: [],
      lostPage: 0,
      lostListData: [],
      buyPage: 0,
      buyListData: [],
      chatPage: 0,
      chatListPage: [],
    });
    // 获取文章内容
    that.getCommunity(-1, 0);
  },


  onShow: function () {

    var that = this;
    // 按钮状态
    var animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-in-out',
    })
    animation.opacity(1).step()
    this.setData({
      postAnimation: animation.export()
    })

  },

  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    var that = this;
    that.emptyPage(that.data.currentNav);
    that.getCommunity(that.data.currentNav, 0);
    wx.stopPullDownRefresh();
  },

  onReachBottom: function () {
    // 页面上拉触底事件的处理函数
    var that = this;
    var newPage = that.addPage(that.data.currentNav);
    that.getCommunity(that.data.currentNav, newPage);
  },

  // 根据当前位置清空页面
  emptyPage: function (currentNav) {
    var that = this;
    switch (parseInt(currentNav)) {
      case 0:
        that.setData({
          allPage: 0,
          allListData: [],
        });
        break;
      case 1:
        that.setData({
          lostPage: 0,
          lostListData: [],
        });
        break;
      case 2:
        that.setData({
          buyPage: 0,
          buyListData: [],
        });
        break;
      case 3:
        that.setData({
          chatPage: 0,
          chatListPage: [],

        });
        break;
    }
  },


  // 根据当前位置扩展第几页面
  addPage: function (currentNav) {
    var that = this;
    var page = 0;
    switch (parseInt(currentNav)) {
      case 0:
        page = that.data.allPage + 1
        that.setData({
          allPage: page
        });
        break;
      case 1:
        page = that.data.lostPage + 1

        that.setData({
          lostPage: page
        });
        break;
      case 2:
        page = that.data.buyPage + 1
        that.setData({
          buyPage: page
        });
        break;
      case 3:
        page = that.data.chatPage + 1
        that.setData({
          chatPage: page
        });
        break;
    }

    return page;
  },



  // 根据当前位置选择获取的文章内容
  getCommunity: function (currentNav, page) {
    var that = this;
    switch (parseInt(currentNav)) {
      case 0:
        that.getAllCommunity(page);
        break;
      case 1:
        that.getLostCommunity(page);
        break;
      case 2:
        that.getBuyCommunity(page);
        break;
      case 3:
        that.getChatCommunity(page);
        break;
      case -1: // 获取所有
        that.getAllCommunity(page);
        that.getLostCommunity(page);
        that.getBuyCommunity(page);
        that.getChatCommunity(page);
        break;
    }
  },



  // 获取全部文章内容
  getAllCommunity: function (page) {
    var that = this;
    API.getAllCommunity(page)
      .then((res) => {
        // console.log(res);
        if (res.data.error == Config.ERR.OK) {
          if (that.data.data == '') {
            that.setData({
              allListData: res.data.data
            })
          } else {
            var newdata = that.data.allListData
            newdata.push(...res.data.data);
            that.setData({
              allListData: newdata
            })
          }
        } else if (res.data.error == Config.ERR.ER) {
          // Util.UI.loading(1000, res.data.data);
        }
      });
  },



  // 获取失物招领内容
  getLostCommunity: function (page) {
    var that = this;
    API.getLostCommunity(page)
      .then((res) => {
        // console.log(res);
        if (res.data.error == Config.ERR.OK) {
          if (that.data.data == '') {
            that.setData({
              lostListData: res.data.data
            })
          } else {
            var newdata = that.data.lostListData
            newdata.push(...res.data.data);
            that.setData({
              lostListData: newdata
            })
          }
        } else if (res.data.error == Config.ERR.ER) {
          // Util.UI.loading(1000, res.data.data);
        }
      });
  },



  // 获取二手交易内容
  getBuyCommunity: function (page) {
    var that = this;
    API.getBuyCommunity(page)
      .then((res) => {
        // console.log(res);
        if (res.data.error == Config.ERR.OK) {
          if (that.data.data == '') {
            that.setData({
              buyListData: res.data.data
            })
          } else {
            var newdata = that.data.buyListData
            newdata.push(...res.data.data);
            that.setData({
              buyListData: newdata
            })
          }
        } else if (res.data.error == Config.ERR.ER) {
          // Util.UI.loading(1000, res.data.data);
        }
      });
  },


  // 获取吐槽内容
  getChatCommunity: function (page) {
    var that = this;
    API.getChatCommunity(page)
      .then((res) => {
        // console.log(res);
        if (res.data.error == Config.ERR.OK) {
          if (that.data.data == '') {
            that.setData({
              chatListPage: res.data.data
            })
          } else {
            var newdata = that.data.chatListPage
            newdata.push(...res.data.data);
            that.setData({
              chatListPage: newdata
            })
          }
        } else if (res.data.error == Config.ERR.ER) {
          // Util.UI.loading(1000, res.data.data);
        }
      });
  },

  // 获取搜索内容
  getCommunitySearch: function (content) {
    var that = this;
    API.getCommunitySearch(content)
      .then((res) => {
        // console.log(res);
        if (res.data.error == Config.ERR.OK) {
          that.emptyPage(0);
          var newdata = that.data.allListData
          newdata.push(...res.data.data);
          that.setData({
            allListData: newdata
          })
        } else if (res.data.error == Config.ERR.ER) {
          Util.UI.loading(1000, res.data.data);
        }
      });
  },


  //点击添加按钮
  clickPost: function () {
    var that = this;

    var animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-in-out',
    })
    animation.opacity(0).rotate(180).step()
    this.setData({
      postAnimation: animation.export()
    })

    setTimeout(function () {
      wx.navigateTo({
        url: '../postCommunity/postCommunity',
      })
    }, 100)
  },


  //点击搜索按钮
  clickSearch: function () {
    var that = this;

    // 如果搜素输入框是ture，没有文字，则进行提示
    if (that.data.searchInput == true && that.data.searchInputWorld == '') {
      Util.UI.loading(1000, '请输入搜索的关键字');
    }

    // 如果搜素输入框是false，则展开输入框
    if (that.data.searchInput == false) {
      that.setData({
        searchInput: true
      })
    }

    // 如果搜素输入框是ture，且有文字，则进行搜索
    if (that.data.searchInput == true && that.data.searchInputWorld != '') {
      that.getCommunitySearch(that.data.searchInputWorld);
      // 搜索完就将搜索框消失
      that.setData({
        searchInput: false,
        searchInputWorld: '',
        currentNav: 0,
      })
    }


  },

  // 点击外边框
  clickView: function () {
    var that = this;
    if (that.data.searchInput == true) {
      that.setData({
        searchInput: false
      })
    }
  },

  // 点击图片
  clickImg: function (event) {
    var url = 'http://' + event.currentTarget.dataset.imgurl;
    wx.previewImage({
      urls: [url] // 需要预览的图片http链接列表
    })
  },


  //点击tab切换 
  clickNav: function (e) {
    var that = this;
    if (this.data.currentNav === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentNav: e.target.dataset.current
      })
    }
  },

  //获取输入框的数据
  contactInput: function (event) {
    this.setData({ searchInputWorld: event.detail.value });
  },


})